﻿@for(var i= 0; i< GetValueCount(); i++)
{
    var kv = Value[i];
    var index = i;
    var length = i + 1;
    var total = GetValueCount();
    <MRow NoGutters Class="mt-9">
        <MCol Md="5">
            <STextField Value="kv.Key" Label="Key" HideDetails="@("auto")" Dense Outlined ValueChanged="(string key)=> OnModifyKey(index, key)" />
        </MCol>
        <MCol Md="5" Class="my-auto dynamic-keyvalue">
            <STextField Class="ml-6" Value="kv.Value" Label="Value" HideDetails="@("auto")" Dense Outlined ValueChanged="(string value) => OnModifyValue(index, value)"/>
        </MCol>
        <MCol Md="2" Class="my-auto pl-4 dynamic-keyvalue">
            @if(total > 1)
            {
                <MButton Icon OnClick="()=> OnRemoveKeyValue(index)">
                    <SIcon>
                        mdi-delete-outline
                    </SIcon>
                </MButton>
            }
            @if (length == total)
            {
                <MButton Icon OnClick="OnAddKeyValue">
                    <SIcon>
                        mdi-plus-circle
                    </SIcon>
                </MButton>
            }
        </MCol>
    </MRow>
}

@code {
    [Parameter]
    public List<KeyValuePair<string, string>> Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;

            OnValueChange();
        }
    }

    [Parameter]
    public EventCallback<List<KeyValuePair<string, string>>> ValueChanged { get; set; }

    private List<KeyValuePair<string, string>> _value = new List<KeyValuePair<string, string>>();

    private int GetValueCount()
    {
        return _value.Count();
    }

    private Task OnValueChange()
    {
        if(GetValueCount() == 0)
        {
            _value.Add(new KeyValuePair<string, string>("", ""));
        }

        return Task.CompletedTask;
    }


    private async Task OnModifyKey(int index, string key)
    {
        var oldKeyValuePair = _value[index];

        _value[index] = new KeyValuePair<string, string>(key, oldKeyValuePair.Value);

        if (ValueChanged.HasDelegate)
        {
            await ValueChanged.InvokeAsync(_value);
        }
    }

    private async Task OnModifyValue(int index, string value)
    {
        var oldKeyValuePair = _value[index];

        _value[index] = new KeyValuePair<string, string>(oldKeyValuePair.Key, value);

        if (ValueChanged.HasDelegate)
        {
            await ValueChanged.InvokeAsync(_value);
        }
    }

    private async Task OnAddKeyValue()
    {
        _value.Add(new KeyValuePair<string, string>("", ""));

        if (ValueChanged.HasDelegate)
        {
            await ValueChanged.InvokeAsync(_value);
        }
    }

    private async Task OnRemoveKeyValue(int index)
    {
        _value.RemoveAt(index);

        if (ValueChanged.HasDelegate)
        {
            await ValueChanged.InvokeAsync(_value);
        }
    }
}
